//
// Created by 王筱童 on 2022/5/9.
//

#ifndef TINY_REDIS_SERVER_CMAKE_OTHERS_H
#define TINY_REDIS_SERVER_CMAKE_OTHERS_H
#include <sstream>
#include <vector>
#include <concepts>

template<typename T>
concept integral = std::is_integral<T>::value;
template<integral T>
std::string transfer_to_byte(T value) {
    constexpr std::size_t len = sizeof(T);
    std::string res(len,'\0');
    res.shrink_to_fit();
    for (int i = 0; i < len; i++) {
        res[i] = '\0';
        res[i] |= (static_cast<T>(0xff) & (value >> i));
    }
    return res;
}
template<integral T>
T transfer_from_byte(const std::string& bytes) {
    constexpr std::size_t len = sizeof(T);
    T res = 0;
    for (int i = 0; i < len; i++) {
        res |= (static_cast<T>(bytes[i]) << i);
    }
    return res;
}


template<typename TO>
TO convert(const auto& val){
    TO res;
    std::stringstream con;
    con<<val;
    con>>res;
    return res;
}

extern std::string serialize(const std::string&str);
extern std::pair<std::string,std::string> deserialize(const std::string&bytes);
extern std::pair<std::uint64_t, std::string> deserialize_head(const std::string&bytes);
extern std::string trim(const std::string&str);
extern std::vector<std::string> split(const std::string&str, char ch);
extern std::string join(const std::vector<std::string>& vec, char ch);

extern std::string nil;
extern std::string ok;
#endif //TINY_REDIS_SERVER_CMAKE_OTHERS_H
